package com.kalix.middleware.payment.biz;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.kalix.middleware.payment.api.biz.IPaymentService;
import com.kalix.middleware.payment.api.dto.AlipayPaymentInfo;
import com.kalix.middleware.payment.api.dto.AlipayRefundParam;
import com.kalix.middleware.payment.biz.utils.AlipayAPIFactory;

public class PaymentServiceImpl implements IPaymentService {

    public String goAlipayPay(AlipayPaymentInfo alipayPaymentInfo) {
        AlipayClient client = AlipayAPIFactory.getAlipayClient();
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        // 封装请求支付信息
        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
        model.setOutTradeNo(alipayPaymentInfo.getOut_trade_no());
        model.setProductCode(alipayPaymentInfo.getProduct_code());
        model.setTimeoutExpress(alipayPaymentInfo.getTimeout_express());
        model.setBody(alipayPaymentInfo.getBody());
        model.setSubject(alipayPaymentInfo.getSubject());
        model.setTotalAmount(alipayPaymentInfo.getTotal_amount());
        request.setBizModel(model);
        // 设置异步通知地址
        request.setNotifyUrl(alipayPaymentInfo.getNotify_url());
        // 设置同步地址
        request.setReturnUrl(alipayPaymentInfo.getReturn_url());
        try {
            String result = client.pageExecute(request).getBody();
            result = result.replaceAll("GBK", "UTF-8");
            return result;
        } catch (AlipayApiException e) {
            e.printStackTrace();
            return null;
        }
    }

    public String goAlipayRefund(AlipayRefundParam alipayRefundParam) {
        AlipayClient client = AlipayAPIFactory.getAlipayClient();
        if (alipayRefundParam.getOut_request_no() != null || alipayRefundParam.getTrade_no() != null) {
            AlipayTradeRefundRequest refundRequest = new AlipayTradeRefundRequest();
            AlipayTradeRefundModel model = new AlipayTradeRefundModel();
            model.setOutTradeNo(alipayRefundParam.getOut_trade_no());
            model.setTradeNo(alipayRefundParam.getTrade_no());
            model.setRefundAmount(alipayRefundParam.getRefund_amount());
            model.setRefundReason(alipayRefundParam.getRefund_reason());
            model.setOutRequestNo(alipayRefundParam.getOut_request_no());
            refundRequest.setBizModel(model);
            try {
                AlipayTradeRefundResponse refundResponse = client.execute(refundRequest);
                return refundResponse.getBody();
            } catch (AlipayApiException e) {
                e.printStackTrace();
                return e.toString();
            }
        }
        return null;
    }
}
